// 根据reportId获取reportConfig
function getReportConfigById(contextPath,reportId) {
        $.ajax({
            type: "POST",
            url: contextPath+"/reportConfig/getReportConfigById",
            data: {'id': reportId},  //传入组装的参数
            async: false,
            dataType: "json",
            success: function (result) {
                window.dsl = result.data.dsl;
            }
        })
}

// 获取queryBuilder中dsl查询条件
function getQueryBuilderDsl() {
    var dsl = null;
    if (pageData.queryBuilderObj != null && pageData.queryBuilderObj.queryBuilder('getRules') != null) {
        dsl = JSON.stringify(pageData.queryBuilderObj.queryBuilder('getRules'));
    }
    return dsl;
}

function getQueryBuilderDsls() {
    var dsl = null;
    if (pageData.queryBuilderObj != null && pageData.queryBuilderObj.queryBuilder('getSQL') != null) {
        dsl = JSON.stringify(pageData.queryBuilderObj.queryBuilder('getSQL'));
    }
    return dsl;
}

/**
 * 加载字典值
 */
function dictionary(contextPath, dictType){
    $.ajax({
        type: "POST",
        url: contextPath + "/loadsDict/loadsDictionary",
        data: {'dictType':dictType},  //传入组装的参数
        async: false,
        dataType: "json",
        success: function (result) {
            window.province_dict = result.data;
        }
    });
}

// 初始化queryBuilder
function initBuilder(contextPath) {
    $("#builder_div").empty();
    $("#builder_div").append("<div id='builder'></div>");
    var reportId = $('#reportId').val();
    $.ajax({
        type: "POST",
        url: contextPath + "/showReport/getQueryBuilderByReportId",
        data: {reportId: reportId},
        async: false,
        success: function (result) {
            var filters = [];
            var data = result.data;
            for (var i = 0; i < data.length; i++) {
                if ('select' == data[i].input) {
                    data[i].valueSetter = function(rule, value) {
                        rule.$el.find('.rule-value-container select')[0].selectize.setValue(value);
                    };
                }
                filters.push(data[i]);
            }
            pageData.queryBuilderObj = $('#builder').queryBuilder({
                allow_empty: true,
                // default_filter: 'name',
                sort_filters: true,
                optgroups: {
                    core: {
                        en: 'Core'
                    }
                },

                plugins: {
                    'bt-tooltip-errors': { delay: 100 },
                    'sortable': null,
                    'filter-description': { mode: 'bootbox' },
                    // 'bt-selectpicker': '',
                    'unique-filter': null,
                    'bt-checkbox': { color: 'primary' },
                    'invert': null,
                    // 'not-group': null
                },

                // standard operators in custom optgroups
                operators: [
                    { type: 'equal', optgroup: '基本类型' },
                    { type: 'not_equal', optgroup: '基本类型' },
                    { type: 'in', optgroup: '基本类型' },
                    { type: 'not_in', optgroup: '基本类型' },
                    { type: 'less', optgroup: '数字类型' },
                    { type: 'less_or_equal', optgroup: '数字类型' },
                    { type: 'greater', optgroup: '数字类型' },
                    { type: 'greater_or_equal', optgroup: '数字类型' },
                    { type: 'between', optgroup: '数字类型' },
                    { type: 'not_between', optgroup: '数字类型' },
                    { type: 'begins_with', optgroup: '字符串类型' },
                    { type: 'not_begins_with', optgroup: '字符串类型' },
                    { type: 'contains', optgroup: '字符串类型' },
                    { type: 'not_contains', optgroup: '字符串类型' },
                    { type: 'ends_with', optgroup: '字符串类型' },
                    { type: 'not_ends_with', optgroup: '字符串类型' },
                    { type: 'is_empty' },
                    { type: 'is_not_empty' },
                    { type: 'is_null' },
                    { type: 'is_not_null' }
                ],
                filters: filters
            });

            $('#builder').on('afterCreateRuleInput.queryBuilder', function(e, rule) {
                if (rule.filter.plugin == 'selectize') {
                    rule.$el.find('.rule-value-container').css('min-width', '200px')
                        .find('.selectize-control').removeClass('form-control');
                }
            });
        }
    });
}


